//
//  HWWebUtils.m
//  94hwan
//
//  Created by 金石 on 2018/4/28.
//  Copyright © 2018年 孔祥刚. All rights reserved.
//
//请求类

#import "HWWebUtils.h"

#define timeOut 60

@implementation HWWebUtils
+ (instancetype)sharedManager {
    static dispatch_once_t once;
    static id instance;
    dispatch_once(&once, ^{
        //#warning 基地址
        //        instance = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.bing.com"]];
        instance = [[self alloc] init];
    });
    return instance;
}

- (instancetype)init {
    {
        if ((self = [super init])) {
            // 设置超时时间，afn默认是60s
            self.requestSerializer.timeoutInterval = timeOut;
            // 响应格式添加text/plain
            self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html", nil];
            self.responseSerializer = [AFHTTPResponseSerializer serializer];
            [self.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
//            [self setSecurityPolicy:[self customSecurityPolicy]];
            self.securityPolicy.allowInvalidCertificates = YES;
            [self.securityPolicy setValidatesDomainName:NO];
            // 监听网络状态,每当网络状态发生变化就会调用此block
            [self.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
                switch (status) {
                    case AFNetworkReachabilityStatusNotReachable:
                    {
                        // 无连线
                        [SVProgressHUD showInfoWithStatus:@"当前无网络链接"];
                        
                    }
                        break;
                    case AFNetworkReachabilityStatusReachableViaWWAN:               // 蜂窝网络
                        DLog(@"AFNetworkReachability Reachable via WWAN");
                        break;
                    case AFNetworkReachabilityStatusReachableViaWiFi:               // WiFi
                        DLog(@"AFNetworkReachability Reachable via WiFi");
                        break;
                    case AFNetworkReachabilityStatusUnknown:                        // 未知网络
                    default:
                        DLog(@"AFNetworkReachability Unknown");
                        break;
                }
            }];
            // 开始监听
            [self.reachabilityManager startMonitoring];
        }
        return self;
    }
}

/**
 获得网络状态
 
 @return
 AFNetworkReachabilityStatusUnknown          = -1,
 AFNetworkReachabilityStatusNotReachable     = 0,
 AFNetworkReachabilityStatusReachableViaWWAN = 1,
 AFNetworkReachabilityStatusReachableViaWiFi = 2,
 */
+ (int)getNetworkStatus {
    return [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
}

/**
 get
 
 @param parameters 参数
 @param progress   进度
 @param success    成功回调
 @param failure    失败回调
 */
+ (void)doGetWithParameters:(nullable id)parameters progress:(void(^ __nullable)(CGFloat progress))progress success:(void(^ _Nonnull)(id response))success failure:(void(^ _Nonnull)(NSString *error))failure {
    
    NSString *url = [HWGlobalConst baseUrl];
    NSMutableDictionary *mutableDic = [parameters mutableCopy];
#ifdef DEBUG
    //打印url
    NSArray *allKeys = [mutableDic allKeys];
    NSArray *allValues = [mutableDic allValues];
    NSString *logUrl = @"";
    for (int i = 0; i < allKeys.count; i ++) {
        if (i == 0) {
            logUrl = [NSString stringWithFormat:@"%@?%@=%@",[HWGlobalConst baseUrl],allKeys[i],allValues[i]];
        } else {
            logUrl = [NSString stringWithFormat:@"%@&%@=%@",logUrl,allKeys[i],allValues[i]];
        }
    }
    NSLog(@"url:%@",logUrl);
#endif
    [[self sharedManager] GET:url   parameters:mutableDic  headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        if (progress) {
            progress(downloadProgress.fractionCompleted);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            success(dic);
        }else {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([error localizedDescription]);
    }];
}

// 搜索

+ (void)doGetWithSearchParameters:(nullable id)parameters progress:(void(^ __nullable)(CGFloat progress))progress success:(void(^ _Nonnull)(id response))success failure:(void(^ _Nonnull)(NSString *error))failure {
    
    NSString *url = [HWGlobalConst baseUrl];
    NSMutableDictionary *mutableDic = [parameters mutableCopy];
#ifdef DEBUG
    //打印url
    NSArray *allKeys = [mutableDic allKeys];
    NSArray *allValues = [mutableDic allValues];
    NSString *logUrl = @"";
    for (int i = 0; i < allKeys.count; i ++) {
        if (i == 0) {
            logUrl = [NSString stringWithFormat:@"%@?%@=%@",[HWGlobalConst baseUrl],allKeys[i],allValues[i]];
        } else {
            logUrl = [NSString stringWithFormat:@"%@&%@=%@",logUrl,allKeys[i],allValues[i]];
        }
    }
    NSLog(@"url:%@",logUrl);
#endif
    [[self sharedManager] GET:url parameters:mutableDic headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        if (progress) {
            progress(downloadProgress.fractionCompleted);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            success(dic);
        }else {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([error localizedDescription]);
    }];
}
/**
 post
 
 @param parameters 参数
 @param progress   进度
 @param success    成功回调
 @param failure    失败回调
 */
+ (void)doPostWithParameters:(nullable id)parameters progress:(void(^ __nullable)(CGFloat progress))progress success:(void(^ _Nonnull)(id response))success failure:(void(^ _Nonnull)(NSString *error))failure {
    NSString *url = [HWGlobalConst baseUrl];

    NSMutableDictionary *mutableDic = [parameters mutableCopy];
#ifdef DEBUG
    //打印url
    NSArray *allKeys = [mutableDic allKeys];
    NSArray *allValues = [mutableDic allValues];
    NSString *logUrl = @"";
    for (int i = 0; i < allKeys.count; i ++) {
        if (i == 0) {
            logUrl = [NSString stringWithFormat:@"%@?%@=%@",[HWGlobalConst baseUrl],allKeys[i],allValues[i]];
        } else {
            logUrl = [NSString stringWithFormat:@"%@&%@=%@",logUrl,allKeys[i],allValues[i]];
        }
    }
    NSLog(@"url:%@",logUrl);
#endif
    [[self sharedManager] POST:url parameters:mutableDic  headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress.fractionCompleted);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            success(dic);
        }else {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([error localizedDescription]);
    }];
    
    
  
}

// URL
+ (void)doGetWithURL:(nullable id)parameters progress:(void(^ __nullable)(CGFloat progress))progress success:(void(^ _Nonnull)(id response))success failure:(void(^ _Nonnull)(NSString *error))failure {
    
    NSString *url = @"https://jia.xmwan.com/weix/downtz.php?apk=http://d.xmwan.com/xx/59859e99e5c08e2542fbd5fdd9f31762_xmw02566_2900.apk";
    NSMutableDictionary *mutableDic = [parameters mutableCopy];
#ifdef DEBUG
    //打印url
//    NSArray *allKeys = [mutableDic allKeys];
//    NSArray *allValues = [mutableDic allValues];
//    NSString *logUrl = @"";
//    for (int i = 0; i < allKeys.count; i ++) {
//        if (i == 0) {
//            logUrl = [NSString stringWithFormat:@"%@?%@=%@",url,allKeys[i],allValues[i]];
//        } else {
//            logUrl = [NSString stringWithFormat:@"%@&%@=%@",logUrl,allKeys[i],allValues[i]];
//        }
//    }
//    NSLog(@"url:%@",logUrl);
#endif
    [[self sharedManager] GET:url parameters:mutableDic headers:nil  progress:^(NSProgress * _Nonnull downloadProgress) {
        if (progress) {
            progress(downloadProgress.fractionCompleted);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            success(dic);
        }else {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([error localizedDescription]);
    }];
    
    
    
}

// 客户的域名

+ (void)doGetWithCustomerParameters:(nullable id)parameters progress:(void(^ __nullable)(CGFloat progress))progress success:(void(^ _Nonnull)(id response))success failure:(void(^ _Nonnull)(NSString *error))failure {
    
    NSString *url = @"http://www.lyx998.com";
    NSMutableDictionary *mutableDic = [parameters mutableCopy];
#ifdef DEBUG
    //打印url
    NSArray *allKeys = [mutableDic allKeys];
    NSArray *allValues = [mutableDic allValues];
    NSString *logUrl = @"";
    for (int i = 0; i < allKeys.count; i ++) {
        if (i == 0) {
            logUrl = [NSString stringWithFormat:@"%@?%@=%@",[HWGlobalConst baseUrl],allKeys[i],allValues[i]];
        } else {
            logUrl = [NSString stringWithFormat:@"%@&%@=%@",logUrl,allKeys[i],allValues[i]];
        }
    }
    NSLog(@"url:%@",logUrl);
#endif
    [[self sharedManager] GET:url parameters:mutableDic headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        if (progress) {
            progress(downloadProgress.fractionCompleted);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            success(dic);
        }else {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([error localizedDescription]);
    }];
}


// 支付宝支付
+ (void)doGetWithAliPayParameters:(id)parameters progress:(void (^)(CGFloat))progress success:(void (^)(id))success failure:(void (^)(NSString *))failure {
    
    //http://shouyou.94php.com/pay/trade/alipay.php?suid=47&price=1&goods_id=1
    
    NSString *url = [NSString stringWithFormat:@"%@pay/trade/alipay.php", [HWGlobalConst baseUrl]];
    NSMutableDictionary *mutableDic = [parameters mutableCopy];
#ifdef DEBUG
    //打印url
    NSArray *allKeys = [mutableDic allKeys];
    NSArray *allValues = [mutableDic allValues];
    NSString *logUrl = @"";
    for (int i = 0; i < allKeys.count; i ++) {
        if (i == 0) {
            logUrl = [NSString stringWithFormat:@"%@?%@=%@",url,allKeys[i],allValues[i]];
        } else {
            logUrl = [NSString stringWithFormat:@"%@&%@=%@",logUrl,allKeys[i],allValues[i]];
        }
    }
    NSLog(@"url:%@",logUrl);
#endif
    [[self sharedManager] GET:url  parameters:mutableDic headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        if (progress) {
            progress(downloadProgress.fractionCompleted);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            success(dic);
        }else {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([error localizedDescription]);
    }];
}

// 微信支付
+ (void)doGetWithWXPayParameters:(id)parameters progress:(void (^)(CGFloat))progress success:(void (^)(id))success failure:(void (^)(NSString *))failure {
    
    //http://shouyou.94php.com/pay/trade/wx.php?suid=77&money=56.00&goods_id=15
    NSString *url = [NSString stringWithFormat:@"%@pay/trade/wx.php", [HWGlobalConst baseUrl]];
    NSMutableDictionary *mutableDic = [parameters mutableCopy];
#ifdef DEBUG
    //打印url
    NSArray *allKeys = [mutableDic allKeys];
    NSArray *allValues = [mutableDic allValues];
    NSString *logUrl = @"";
    for (int i = 0; i < allKeys.count; i ++) {
        if (i == 0) {
            logUrl = [NSString stringWithFormat:@"%@?%@=%@",url,allKeys[i],allValues[i]];
        } else {
            logUrl = [NSString stringWithFormat:@"%@&%@=%@",logUrl,allKeys[i],allValues[i]];
        }
    }
    NSLog(@"url:%@",logUrl);
#endif
    [[self sharedManager] GET:url  parameters:mutableDic headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        if (progress) {
            progress(downloadProgress.fractionCompleted);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            success(dic);
        }else {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([error localizedDescription]);
    }];
}


// 上传图片
@end
